package com.himalayan.bear;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DirtyData {

    private static CreateIDCardNo createIDCardNo = new CreateIDCardNo();
    private static Random r = new Random();
    private static int clen = Constant.colleges.length;
    private static int mlen = Constant.majors.length;

    public static String college() {
        int index = r.nextInt(clen);
        int index2 = r.nextInt(mlen);
        return Constant.colleges[index];
    }

    public static String major() {
        int index = r.nextInt(clen);
        int index2 = r.nextInt(mlen);
        return Constant.majors[index2].replace("(特设专业)", "");
    }


    public static void main(String[] args) {
        try {
            //核心应用类
            HttpClient httpClient = new DefaultHttpClient();
            for (int i = 1; i <= 1000000; i++) {

                //设定表单需要提交的参数
                List<NameValuePair> qparams = new ArrayList<>();

                //示例：提交用户名和密码
                qparams.add(new BasicNameValuePair("fid", "334263"));
                qparams.add(new BasicNameValuePair("uid", "11139239"));
                qparams.add(new BasicNameValuePair("encryptuid", ""));
                qparams.add(new BasicNameValuePair("platform", ""));
                qparams.add(new BasicNameValuePair("channel", ""));
                qparams.add(new BasicNameValuePair("chan", ""));
                qparams.add(new BasicNameValuePair("mode", "浏览器"));
                qparams.add(new BasicNameValuePair("publicOpenid", ""));
                qparams.add(new BasicNameValuePair("mobile", ""));
                qparams.add(new BasicNameValuePair("code", ""));

                qparams.add(new BasicNameValuePair("content[f_4122007][]", getName()));
                qparams.add(new BasicNameValuePair("content[f_4122008][]", getTel()));
                qparams.add(new BasicNameValuePair("content[f_4122010][]", createIDCardNo.getRandomID()));
                qparams.add(new BasicNameValuePair("content[f_4122011][]", college()));
                qparams.add(new BasicNameValuePair("content[f_4122013][]", major()));

                //设定需要访问的URL，第四个参数为表单提交路径
                URI uri = URIUtils.createURI("https", "www.workec.com", -1,
                        "/form/commit",
                        //将参数加入URL中
                        URLEncodedUtils.format(qparams, "UTF-8"), null);
                //Post提交
                HttpPost httpPost = new HttpPost(uri);

                //System.out.println(httpPost.getURI());

                //httpClient执行，返回response
                HttpResponse response = httpClient.execute(httpPost);

                //获取实体
                HttpEntity httpEntity = response.getEntity();

                //打印StatusLine
                System.out.print("StatusLine: " + response.getStatusLine());

                //读取内容
                String content = EntityUtils.toString(httpEntity, "UTF-8");
                //打印输出结果内容
                System.out.println(" - " + content);
                System.out.println("第" + i + "条");
                if (i % 100 == 0) {
                    try {
                        Thread.sleep(301000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (URISyntaxException | IOException e) {
            e.printStackTrace();
        }
    }

    public static String getName() {
        Random random = new Random();
        String[] Surname = {"赵", "钱", "孙", "李", "周", "吴", "郑", "王", "冯", "陈", "褚", "卫", "蒋", "沈", "韩", "杨", "朱", "秦", "尤", "许",
                "何", "吕", "施", "张", "孔", "曹", "严", "华", "金", "魏", "陶", "姜", "戚", "谢", "邹", "喻", "柏", "水", "窦", "章", "云", "苏", "潘", "葛", "奚", "范", "彭", "郎",
                "鲁", "韦", "昌", "马", "苗", "凤", "花", "方", "俞", "任", "袁", "柳", "酆", "鲍", "史", "唐", "费", "廉", "岑", "薛", "雷", "贺", "倪", "汤", "滕", "殷",
                "罗", "毕", "郝", "邬", "安", "常", "乐", "于", "时", "傅", "皮", "卞", "齐", "康", "伍", "余", "元", "卜", "顾", "孟", "平", "黄", "和",
                "穆", "萧", "尹", "姚", "邵", "湛", "汪", "祁", "毛", "禹", "狄", "米", "贝", "明", "臧", "计", "伏", "成", "戴", "谈", "宋", "茅", "庞", "熊", "纪", "舒",
                "屈", "项", "祝", "董", "梁", "杜", "阮", "蓝", "闵", "席", "季"};
        String girl = "秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希宁欣飘育滢馥筠柔竹霭凝晓欢霄枫芸菲寒伊亚宜可姬舒影荔枝思丽 ";
        String boy = "伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家致树炎德行时泰盛雄琛钧冠策腾楠榕风航弘";
        int index = random.nextInt(Surname.length - 1);
        String name = Surname[index]; //获得一个随机的姓氏
        int i = random.nextInt(3);//可以根据这个数设置产生的男女比例
        if (i == 2) {
            int j = random.nextInt(girl.length() - 2);
            if (j % 2 == 0) {
                name = name + girl.substring(j, j + 2);
            } else {
                name = name + girl.substring(j, j + 1);
            }

        } else {
            int j = random.nextInt(girl.length() - 2);
            if (j % 2 == 0) {
                name = name + boy.substring(j, j + 2);
            } else {
                name = name + boy.substring(j, j + 1);
            }

        }

        return name;
    }

    /**
     * 返回手机号码
     */
    private static String[] telFirst = "134,135,136,137,138,139,150,151,152,157,158,159,130,131,132,155,156,133,153".split(",");

    private static String getTel() {
        int index = getNum(0, telFirst.length - 1);
        String first = telFirst[index];
        String second = String.valueOf(getNum(1, 888) + 10000).substring(1);
        String thrid = String.valueOf(getNum(1, 9100) + 10000).substring(1);
        return first + second + thrid;
    }

    public static int getNum(int start, int end) {
        return (int) (Math.random() * (end - start + 1) + start);
    }
}
